package org.lib93.com.libSkin.utils

import android.content.Context
import android.content.pm.PackageManager.GET_ACTIVITIES
import android.content.res.AssetManager
import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.text.TextUtils

/**
 * 资源管理器
 * 宿主的资源管理器
 * 插件资源管理器
 */
class SkinResources {
    private var context: Context
    private var mResources:Resources //宿主资源管理器
    private var mSkinResources:Resources? = null  //插件资源管理器
    private var mSkinPackageName:String? = null  //插件资源包名信息
    private var isDefaultSkin = true //是否使用默认皮肤
    private constructor(context: Context){
        this.context = context
        mResources = context.resources //获取宿主资源管理器
    }
    companion object{
        @Volatile private var instance: SkinResources? = null
        fun getInstance(context: Context) =
            instance ?: synchronized(this) {
                instance ?: SkinResources(context).also { instance = it }
            }
    }

    /**
     * 初始化皮肤插件的Resources
     */
    fun initSkinResources(path:String):String?{
        try {
            //通过反射获取资源管理对象
            var assetManager = AssetManager::class.java.newInstance()
            //获取加载路径方法队形
            var addAssetPath = assetManager.javaClass.getMethod("addAssetPath",String::class.java)
            addAssetPath.invoke(assetManager,path) //调用方法
            //利用当前设备的resoureces对象获取 插件的Resources对象
            mSkinResources = Resources(assetManager,mResources.displayMetrics,mResources.configuration)
            //获取插件包 包名信息
            val packageArchiveInfo = context.packageManager.getPackageArchiveInfo(path, GET_ACTIVITIES)
            mSkinPackageName = packageArchiveInfo?.packageName?:null
            //是否使用默认皮肤
            //是否使用默认皮肤
            isDefaultSkin = TextUtils.isEmpty(mSkinPackageName) || mResources == null
            return mSkinPackageName
        }catch (e:Exception){
            return null
        }
        return null

    }

    fun getBackground(id:Int):Any{
        //从宿主中根据ｉｄ ＠12213 　获取名称　R.colco.red
        val resourceTypeName = mResources.getResourceTypeName(id)
        if ("color".equals(resourceTypeName))
            return getColor(id)
        else
            return getDrawable(id)
    }
    /**
     * 输入主APP的ID，到皮肤APK文件中去找到对应ID的颜色值
     * @param resId
     * @return
     */
     fun getColor(id: Int):Int {
        if (isDefaultSkin)
            return mResources.getColor(id)
        val newId = getIdentifier(id)
        if(null == newId || 0 == newId){
            return mResources.getColor(id)
        }else
            return  mSkinResources?.getColor(newId)!!
    }

    fun getColorStateList(resId: Int): ColorStateList? {
        if (isDefaultSkin) {
            return mResources.getColorStateList(resId)
        }
        val skinId = getIdentifier(resId)!!
        return if (skinId == 0) {
            mResources.getColorStateList(resId)
        } else mSkinResources!!.getColorStateList(skinId)
    }


    fun getDrawable(id: Int):Drawable{
        if (isDefaultSkin)
            return mResources.getDrawable(id)
        val newId = getIdentifier(id)

        if(null == newId || 0 == newId){
            return mResources.getDrawable(id)
        }else
            return  mSkinResources?.getDrawable(newId)!!
    }
    /**
     * 1.通过原始app中的resId(R.color.XX)获取到自己的 名字
     * 2.根据名字和类型获取皮肤包中的ID
     */
    private fun getIdentifier(id: Int):Int?{
        if (isDefaultSkin)
            return id
        val name = mResources.getResourceEntryName(id)
        val type = mResources.getResourceTypeName(id)
        return mSkinResources?.getIdentifier(name,type,mSkinPackageName)
    }

    /**
     * 回收插件资源
     */
    fun reset(){
        mSkinResources = null
        isDefaultSkin = true
    }
}